local hx__rugu = fk.CreateSkill {
  name = "hx__rugu",
}

local HX = require "packages/huixiangkill/hxfs"

hx__rugu:addEffect("active", {
  name = "hx__rugu",
  anim_type = "control",
  prompt = "选择一项执行",
  card_num = 0,
  target_num = 0,
  interaction = function(self, player)
    local all_choices = {"hx__rugu1", "hx__rugu2"}
    local choices = table.filter(all_choices, function(choice)
      return not table.contains(player:getTableMark("hx__rugu-phase"), choice)
    end)
    return UI.ComboBox { choices = choices, all_choices = all_choices }
  end,
  can_use = function(self, player)
    return #player:getTableMark("hx__rugu-phase") < 2
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    local p = player
    room:addTableMark(player, "hx__rugu-phase", self.interaction.data)
    if self.interaction.data == "hx__rugu1" then
      local p_num
      if p:getTableMark("@hx__cqbd_record")[2] ~= nil then
        p_num = p:getTableMark("@hx__cqbd_record")[2]
      else
        p_num = p:getHandcardNum()
      end
      room:setPlayerMark(p, "@hx__cqbd_record", {p.hp, p_num})
    else
      local p_hp
      if p:getTableMark("@hx__cqbd_record")[1] ~= nil then
        p_hp = p:getTableMark("@hx__cqbd_record")[1]
      else
        p_hp = p.hp
      end
      room:setPlayerProperty(p, "hp", p_hp)
      HX.SetSkillsTimes(room,p,0)
    end
    room:addPlayerMark(player, "hx__rugu-remove", 1)
  end,
})

hx__rugu:addEffect(fk.TurnEnd, {
  mute = true,
  is_delay_effect = true,
  can_refresh = function(self, event, target, player, data)
    if not player:hasSkill(hx__rugu.name) then return end
    return player:getMark("hx__rugu-remove") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@hx__cqbd_record", 0)
    room:setPlayerMark(player, "hx__rugu-remove", 0)
  end,
})
return hx__rugu
